package com.example.task.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.task.R;


/**
 * 使用适配器实现一个ListView多个不同风格的list显示。
 * 最上面显示应用简介
 * 中间显示应用相关图片
 * 最下面显示应用详细介绍
 * @author ssrrxx
 *
 */
public class AppDetailListViewBaseAdapter extends BaseAdapter {
    private List<Map<String, Object>> list;
    private LayoutInflater inflater;
    //这里type不能用123，否则会越界
    //private static final int TYPE_1 = 0;
    //private static final int TYPE_2 = 1;
    //private static final int TYPE_3 = 2;
    private Context context;

    public AppDetailListViewBaseAdapter(
            List<Map<String, Object>> list, Context context) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    /**
     * 返回布局类型种数，布局会根据种数与下面的方法配合具体怎样实现布局
     */
    @Override
    public int getViewTypeCount() {
        return list.size();
    }

    /**
     * 根据item所在位置返回布局类型
     * 该方法的作用是，根据上中下的位置，可能有超过4中的布局类型。分别调用不同的布局类型，在getView方法中根据TYPE进行相应的布局
     */
    @Override
    public int getItemViewType(int position) {
        /*if (position  == 0) {
            return TYPE_1;
        } else if (position  == 1) {
            return TYPE_2;
        } else {
            return TYPE_3;
        }*/
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;
        ViewHolder3 holder3 = null;

        int type = this.getItemViewType(position);

        convertView = inflater.inflate(R.layout.activity_socket_item, null);
        holder1 = new ViewHolder1();
        holder1.image1 = (ImageView) convertView.findViewById(R.id.ImageView1);
        holder1.image2 = (ImageView) convertView.findViewById(R.id.ImageView2);
//        System.out.println(holder1.ico);
        holder1.data1 = (TextView) convertView
                .findViewById(R.id.data1);
        holder1.data2 = (TextView) convertView
                .findViewById(R.id.data2);
        holder1.name1 = (TextView) convertView
                .findViewById(R.id.name1);
        holder1.name2 = (TextView) convertView
                .findViewById(R.id.name2);
//        holder1.appClass = (TextView) convertView
//                .findViewById(R.id.app_class);
        convertView.setTag(holder1);

        if ( list.get(position).get("image1") == null){
            holder1.image1.setVisibility(View.GONE);
            holder1.image2.setImageResource((Integer) list.get(position).get("image2"));
            holder1.name1.setVisibility(View.GONE);
            holder1.name2.setText((String) list.get(position).get("name2"));
            holder1.data1.setVisibility(View.GONE);
            holder1.data2.setText((String) list.get(position).get("data2"));
        }else{
            holder1.image1.setImageResource((Integer) list.get(position).get(
                    "image1"));
            holder1.image2.setVisibility(View.GONE);
            holder1.name1.setText((String)list.get(position).get("name1"));
            holder1.name2.setVisibility(View.GONE);
            holder1.data1.setText((String) list.get(position).get("data1"));
            holder1.data2.setVisibility(View.GONE);

        }

        /*switch (type) {
            case TYPE_1:

                break;
            case TYPE_2:
                convertView = inflater.inflate(R.layout.app_detail_2, null);
                holder2 = new ViewHolder2();
                holder2.gallery = (Gallery) convertView.findViewById(R.id.app_pics);
                int[] imageIds = (int[]) list.get(position).get("imageIds");
                MyGalleryBaseAdapter adapter = new MyGalleryBaseAdapter(imageIds,
                        context);
                holder2.gallery.setAdapter(adapter);
                Log.v("type", "type2");
                break;
            case TYPE_3:
                convertView = inflater.inflate(R.layout.app_detail_3, null);
                holder3 = new ViewHolder3();
                holder3.appIntroduction = (TextView) convertView
                        .findViewById(R.id.app_introduction);
                Log.v("type", "type3");
                holder3.appIntroduction.setText((String) list.get(position).get(
                        "appIntroduction"));
        }*/

        return convertView;
    }

    static class ViewHolder1 {
        ImageView image1;
        ImageView image2;
        TextView name1;
        TextView name2;
        TextView data1;
        TextView data2;
        //TextView appClass;
    }

    static class ViewHolder2 {
        Gallery gallery;
    }

    static class ViewHolder3 {
        TextView appIntroduction;
    }
}